package com.studykun.client.ui;

import java.util.Iterator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class SimplePane extends SimplePanel {
  public class RootPanel extends SimplePanel {
    @Override
    public void doAttachChildren() {
      super.doAttachChildren();
    }

    @Override
    public void doDetachChildren() {
      super.doDetachChildren();
    }
  }
  private RootPanel rootPanel_ = new RootPanel();
  private FlexTable chrome_ = new FlexTable();
  final SimplePanel chromeNN_ = new SimplePanel();
  final SimplePanel chromeNE_ = new SimplePanel();
  final SimplePanel chromeEE_ = new SimplePanel();
  final SimplePanel chromeSE_ = new SimplePanel();
  final SimplePanel chromeSS_ = new SimplePanel();
  final SimplePanel chromeSW_ = new SimplePanel();
  final SimplePanel chromeWW_ = new SimplePanel();
  final SimplePanel chromeNW_ = new SimplePanel();
  final SimplePanel chromeCC_ = new SimplePanel();
  final InlineLabel titleLabel_ = new InlineLabel();
  
  final FlowPanel caption_ = new FlowPanel();

  public SimplePane() {
    super.setWidget(rootPanel_);
    final Anchor closeButton = new Anchor("x");
    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(final ClickEvent event) {
        SimplePane.this.close();
      }
    });
    
    rootPanel_.add(chrome_);
    
    chrome_.setCellPadding(0);
    chrome_.setCellSpacing(0);
    
    chrome_.setWidget(0, 1, chromeNN_);
    chrome_.setWidget(0, 2, chromeNE_);
    chrome_.setWidget(1, 2, chromeEE_);
    chrome_.setWidget(2, 2, chromeSE_);
    chrome_.setWidget(2, 1, chromeSS_);
    chrome_.setWidget(2, 0, chromeSW_);
    chrome_.setWidget(1, 0, chromeWW_);
    chrome_.setWidget(0, 0, chromeNW_);
    chrome_.setWidget(1, 1, chromeCC_);
    
    chromeNN_.add(caption_);
    caption_.add(titleLabel_);
    caption_.setStyleName("Caption");
    
    chrome_.getRowFormatter().setStyleName(0, "dialogTop");
    chrome_.getRowFormatter().setStyleName(1, "dialogMiddle");
    chrome_.getRowFormatter().setStyleName(2, "dialogBottom");
    
    chrome_.getCellFormatter().setStyleName(0, 1, "dialogTopCenter");
    chrome_.getCellFormatter().setStyleName(0, 2, "dialogTopRight");
    chrome_.getCellFormatter().setStyleName(1, 2, "dialogMiddleRight");
    chrome_.getCellFormatter().setStyleName(2, 2, "dialogBottomRight");
    chrome_.getCellFormatter().setStyleName(2, 1, "dialogBottomCenter");
    chrome_.getCellFormatter().setStyleName(2, 0, "dialogBottomLeft");
    chrome_.getCellFormatter().setStyleName(1, 0, "dialogMiddleLeft");
    chrome_.getCellFormatter().setStyleName(0, 0, "dialogTopLeft");
    chrome_.getCellFormatter().setStyleName(1, 1, "dialogMiddleCenter");
    
    chromeNN_.setStyleName("dialogTopCenterInner");
    chromeNE_.setStyleName("dialogTopRightInner");
    chromeEE_.setStyleName("dialogMiddleRightInner");
    chromeSE_.setStyleName("dialogBottomRightInner");
    chromeSS_.setStyleName("dialogBottomCenterInner");
    chromeSW_.setStyleName("dialogBottomLeftInner");
    chromeWW_.setStyleName("dialogMiddleLeftInner");
    chromeNW_.setStyleName("dialogTopLeftInner");
    chromeCC_.setStyleName("dialogMiddleCenterInner");

    rootPanel_.setStyleName("dialogPane");
  }
  
  /**
   * Adds a widget to this panel.
   * 
   * @param widget
   *  the child widget to be added
   */
  @Override
  public void add(
      final Widget widget) {
    chromeCC_.add(widget);
  }

  /**
   * Gets the panel's child widget.
   * 
   * @return
   *  the child widget, or <code>null</code> if none is present
   */
  @Override
  public Widget getWidget() {
    return chromeCC_.getWidget();
  }

  @Override
  public Iterator<Widget> iterator() {
    return chromeCC_.iterator();
  }

  @Override
  public boolean remove(
      final Widget widget) {
    return chromeCC_.remove(widget);
  }

  /**
   * Sets this panel's widget. Any existing child widget will be removed.
   * 
   * @param widget
   *  the panel's new widget, or <code>null</code> to clear the panel
   */
  @Override
  public void setWidget(
      final Widget widget) {
    chromeCC_.setWidget(widget);
  }
  
  public void close() {
    this.removeFromParent();
  }

  public void setText(
      final String text) {
    titleLabel_.setText(text);
  }

  @Override
  protected void doAttachChildren() {
    rootPanel_.doAttachChildren();
  }

  @Override
  protected void doDetachChildren() {
    rootPanel_.doDetachChildren();
  }
}
